#include "Article.h"

int ArticleList::Find(wxString link)
{
    // loop through and find the matching link
    for(int n=0; (unsigned int)n<list.size(); n++)
        if(list[n].link == link) return n;
    return -1;
}
void ArticleList::Append(wxString link, wxString title, wxString description, wxString creator, wxString mode)
{
    // append the article to the master list
    list.push_back(Article(link, title, description, creator, mode));
}
int ArticleList::Remove(wxString link)
{
    // find the link
    int n;
    n = Find(link);

    // if we didn't find it
    if(n < 0) return -1;

    // if we did find it, remove this element
    list.erase(list.begin()+n);
    return n;
}
void ArticleList::RemoveItem(int item)
{
    list.erase(list.begin()+item);
}

void ArticleList::Clear()
{
    list.clear();
}
int ArticleList::Enable(wxString link)
{
    int n;
    n = Find(link);
    if(n>=0 && (unsigned int)n<list.size()) list[n].mode=wxT("enabled");
    return n;
}
int ArticleList::Disable(wxString link)
{
    int n;
    n = Find(link);
    if(n>=0 && (unsigned int)n<list.size()) list[n].mode=wxT("disabled");
    return n;
}
void ArticleList::Save(wxString path)
{
    wxFile f;

    if(f.Open(path, wxFile::write))
    {
        // write a line for each article in the form link|title|mode
        for(unsigned int n=0; n<list.size(); n++)
        {
            list[n].link.Replace(wxT("|"),   wxT(" "), true);
            list[n].link.Replace(wxT("\n"),  wxT(" "), true);
            list[n].title.Replace(wxT("|"),  wxT(" "), true);
            list[n].title.Replace(wxT("\n"), wxT(" "), true);
            list[n].description.Replace(wxT("|"),  wxT(" "), true);
            list[n].description.Replace(wxT("\n"), wxT(" "), true);
            list[n].creator.Replace(wxT("|"),  wxT(" "), true);
            list[n].creator.Replace(wxT("\n"), wxT(" "), true);
            if(list[n].description == wxT(" ") && list[n].creator == wxT(" ") && list[n].mode == wxT(" "))
                f.Write(list[n].link+wxT("|")+list[n].title+wxT("\n"));
            else
                f.Write(list[n].link+wxT("|")+list[n].title+wxT("|")+list[n].description+wxT("|")+list[n].creator+wxT("|")+list[n].mode+wxT("\n"));
        }
    }
    f.Close();
}
void ArticleList::Load(wxString path)
{
    wxTextFile f;
    wxString line;
    wxArrayString a;

    if(wxFile::Exists(path) && f.Open(path))
    {
        Clear();

        // loop over all the lines in the file
        for(size_t n=0; n<f.GetLineCount(); n++)
        {
            line = f.GetLine(n);

            // split by the delimiter
            a = SplitString(line, wxT("|"));

            // if there are enough elements, append this to the list
            if(a.GetCount()==5) Append(a[0].Strip(), a[1], a[2], a[3], a[4].Strip());
            if(a.GetCount()==2) Append(a[0].Strip(), a[1].Strip(), wxT(" "), wxT(" "), wxT(" ") );
        }
    }
    f.Close();
}
int ArticleList::GetCount()
{
    return (int)list.size();
}
wxString ArticleList::GetLink(wxString title)
{
    int n;

    n=-1;
    for(size_t n=0; n<list.size(); n++)
        if(list[n].title==title) break;

    if(n>=0) return list[n].link;

    return wxT("");
}
void ArticleList::TrimOldArticles(int target_size)
{
    if(target_size < 0) return;
    while(GetCount() > target_size) list.erase(list.begin());
}
